var XData = [];
$(function() {
	getShopInf('1');
	var $divShow = $(".sp_con").children();
	$divShow.each(function(index){
		var sp = "sp" + (index + 1),$this = $(this);
//		$this.hover(,);
		$this.hover(function(){
			_changeBgmStyle(sp,$this);
		},function(){
			_changeBgmStyle1(sp,$this);
		});
	});
	
});

/**
 * 获取商铺基础信息
 * @param type
 */
function getShopInf(type) {
	$.ajax({
		cache: true,
		type: "POST",
		url: path + '/jfShopping/getDetailShopInf.do',
		data: {"type":type},
		async: false,
		error: function(request) {
			alert("获取信息失败");
		},
		success: function(data) {
			data = eval('(' + data + ')');
			appearInf(data);
			_getSummaryByDateType(type);
			var praiseList = data.praiseList,
				middleList = data.middleScoreList,
				badList = data.badScoreList;
			showChart(1,praiseList,middleList,badList);
		}
	});
}

function addUnderline(type){
	if(type=="1"){
		$("#a1").css('border-bottom','2px solid rgb(70, 163, 255)');
		$("#a2").css('border-bottom','none');
		$("#a3").css('border-bottom','none');
		$("#a4").css('border-bottom','none');
	}else if(type=="2"){
		$("#a1").css('border-bottom','none');
		$("#a2").css('border-bottom','2px solid rgb(70, 163, 255)');
		$("#a3").css('border-bottom','none');
		$("#a4").css('border-bottom','none');
	}else if(type=="3"){
		$("#a1").css('border-bottom','none');
		$("#a2").css('border-bottom','none');
		$("#a3").css('border-bottom','2px solid rgb(70, 163, 255)');
		$("#a4").css('border-bottom','none');
	}else if(type=="4"){
		$("#a1").css('border-bottom','none');
		$("#a2").css('border-bottom','none');
		$("#a3").css('border-bottom','none');
		$("#a4").css('border-bottom','2px solid rgb(70, 163, 255)');
	}
	
}

function _getSummaryByDateType(type) {
	var shopId = $("#hidShopId").val();
	$("#dateType").val(type);
	addUnderline(type);
	$.ajax({
		cache: true,
		type: "POST",
		url: path + '/jfShopping/getSummaryByDateType.do',
		data: {"type":type,"shopId":shopId},
		async: false,
		error: function(request) {
			alert("获取信息失败");
		},
		success: function(data) {
			data = eval('(' + data + ')');
			appearInfByDateType(data);//商品销售汇总总分析
		}
	});
}
/**
 * 获取销量信息
 * @param type
 */
function getSummaryOrderInf(type,obj) {
	var shopId = $("#hidShopId").val();
	$(".da1_con").children().removeClass("da_f_click").addClass("da_f");
	$(obj).removeClass("da_f").addClass("da_f_click");
	$.ajax({
		cache: true,
		type: "POST",
		url: path + '/jfShopping/summaryOrder.do',
		data: {"type":type,"shopId":shopId},
		async: false,
		error: function(request) {
			alert("获取信息失败");
		},
		success: function(data) {
			data = eval('(' + data + ')');
			var praiseList = data.praiseList,
			middleList = data.middleScoreList,
			badList = data.badScoreList;
			showChart(type,praiseList,middleList,badList);
		}
	});
}

/**
 * 获取查询信息并显示
 * @param data
 */
function appearInf(data) {
	$("#hUserName").html(data.SHOP_NAME);
	$("#hidShopId").val(data.SHOP_ID);
	//判断商铺是否审核通过
	var shopStatus = data.REGISTER_STATUS,shopStatusDesc = "";
	if(shopStatus == '1') {
		shopStatusDesc = "已开通";
	} else if(shopStatus == '2') {
		shopStatusDesc = "审核未通过";
	} else if(shopStatus == '0') {
		shopStatusDesc = "待审核";
	} else {
		shopStatusDesc = "未申请";
	} 
	//商铺头像
	$("#spanShopStatus").html(shopStatusDesc);
	var shopPic = data.SHOP_LOGO;
	if(shopPic==undefined||shopPic==""){
		shopPic = path+"/download/error/nopicture.jpg";
	}
	var headImg = "<a href='#'><img src='"+shopPic+"' style='height:87px;width:87px;'/></a>";
	$("#headImg").append(headImg);
	// 如果有其中一项验证，则给60分
	if(data.EMAIL_AUTHENTICATION == 1 || data.MOBILE_AUTHENTICATION == 1) {
		$("i[name='authenticationScore']").each(function(index){
			var $this = $(this);
			if(index < 3) {
				$this.attr("class","hd_i1");
			}
		});
		$("#spanAuthentication").html(60);
		var str = "<img src='" + path + "/pages/shopping/images/111_04.png'/>";
		$("#divShowAuthenticationImg").append(str);
	}
	$("#spanLast7DayCnt").html(data.LAST7DAYCNT);
	$("#spanLastMonthCnt").html(data.LASTMONTHCNT);
	$("#spanLastHalfYearCnt").html(data.LASTHALFYEARCNT);
	$("#spanLastYearCnt").html(data.LASTYEARCNT);
	
}
/**
 * 根据日期类型查询汇总信息
 * @param data
 */
function appearInfByDateType(data) {
	console.info(data);
	/* 商品销售汇总分析  start*/
	$("#pVisitNum").html(data.visitNum);//浏览总量
	$("#pOrderNum").html(data.sumOrder);//订单笔数
	$("#pMoneyNum").html(data.sumRealMoney);//总金额数
	$("#pProductNum").html(data.sumSellProductSort);//总售出商品种类数
	
	_transform(data.riseVisitPercentage,'riseVisitPercentage','Img','transform','%');
//	$("#riseVisitPercentage").html(data.riseVisitPercentage);//
	_transform(data.riseSumOrderPercentage,'riseOrderPercentage','Img','transform','%');
//	$("#riseOrderPercentage").html(data.riseSumOrderPercentage);//
	_transform(data.riseSumRealMoneyPercentage,'riseMoneyPercentage','Img','transform','%');
//	$("#riseMoneyPercentage").html(data.riseSumRealMoneyPercentage);//
	_transform(data.riseSumProductSortPercentage,'riseProductPercentage','Img','transform','%');
//	$("#riseProductPercentage").html(data.riseSumProductSortPercentage);//
	/* 商品销售汇总分析  end*/
	
	/* 店铺订单分析  start*/
	$("#spanOrderCnt").html(data.sumOrder);//总订单数量
	$("#spanCompleteCnt").html(data.completeOrder);//已完成订单数量
	$("#spanRtnCnt").html(data.returnOrder);//退货数量
	$("#spanEvaluateCnt").html(data.evaluateOrder);//订单评价数量
	$("#spanPriseCnt").html(data.priseCnt);//好评率
	
	_transform(data.riseOrderCnt,'riseOrderCnt','Img','transform','');
//	$("#riseOrderCnt").html(data.riseOrderCnt);
	_transform(data.riseCompleteOrder,'riseCompleteOrder','Img','transform','');
//	$("#riseCompleteOrder").html(data.riseCompleteOrder);
	_transform(data.riseReturnOrder,'riseReturnOrder','Img','transform','');
//	$("#riseReturnOrder").html(data.riseReturnOrder);
	_transform(data.riseEvaluateOrder,'riseEvaluateOrder','Img','transform','');
//	$("#riseEvaluateOrder").html(data.riseEvaluateOrder);
	_transform(data.risePpriseCnt,'risePpriseCnt','Img','transform','');
//	$("#risePpriseCnt").html(data.risePpriseCnt);
	/* 店铺订单分析  end*/
}

function _transform(dataNum,objId,img,className,dw){
	dataNum = parseFloat(dataNum);
	if(parseFloat(dataNum)<0){
		dataNum = 0-parseFloat(dataNum);
		$("#"+objId+img).addClass(className);
	} else {
		$("#"+objId+img).removeClass(className);
	}
	$("#"+objId).html(dataNum+dw);
}

/**
 * 返回最近7天日期
 * @returns dateArray
 */
function getDateArr(){
	//设置日期，当前日期的前七天
	var myDate = new Date(); //获取今天日期
	myDate.setDate(myDate.getDate() - 7);
	var dateArray = []; 
	var flag = 1; 
	for (var i = 0; i < 7; i++) {
		myDate.setDate(myDate.getDate() + flag);
		var month = myDate.getMonth() + 1;
		month = '00'+month;
		var day = '00'+myDate.getDate();
		var formatDate = myDate.getFullYear() + "-" +month.substr(month.length-2,month.length) + "-" + day.substr(day.length-2,day.length);
		dateArray.push(formatDate);
	}
	dateArray.sort();
	return dateArray;
}

/**
 * 
 * @returns dateArray
 */
function getWeekArr() {
	var dataArray = [];
	var d1 = new Date();
	var d2 = new Date();
	d2.setMonth(0);
	d2.setDate(1);
	var rq = d1-d2;
	var s1 = Math.ceil(rq/(24*60*60*1000));
	var s2 = Math.ceil(s1/7);
	for(var i = 5;i >= 0;i--) {
		if(s2 > i) {
			dataArray.push("第"+(s2 - i)+"周");
		} else {
			
		}
	}
	dataArray.sort();
	return dataArray;
}

/**
 * 获取最近mon个月
 * @param mon
 * @returns {Array}
 */
function getLastMonths(mon){
	var lastMonths = [];
	var today = new Date();  
	today.setMonth(today.getMonth()+1);  
	for(var i=0;i<mon;i++){  
		var lastMonth = today.setMonth(today.getMonth()-1);
		if(today.getMonth()+1 < 10) {
			lastMonths.push(today.getFullYear() + "-0" + (today.getMonth()+1) + "(月)"); 
		} else {
			lastMonths.push(today.getFullYear() + "-" + (today.getMonth()+1) + "(月)"); 
		}
	}
	lastMonths.sort();
	return lastMonths;  
}

function getQuarterArr(){
	var dataArray = [];
	var date = new Date();
	var currMonth = date.getMonth();
	var quar = Math.floor( ( currMonth % 3 == 0 ? ( currMonth / 3 ) : ( currMonth / 3 + 1 ) ) );
	for(var i = 3;i >= 0; i--) {
		if(quar > i) {
			dataArray.push(date.getFullYear() + "-" + (quar - i) + "(季)");
		} else {
			dataArray.push((date.getFullYear() - 1) + "-" + (i + 1) + "(季)");
		}
	}
	dataArray.sort();
	return dataArray;
}



/**
 * 判断数组data中是否存在值，若不存在在添加
 * @param data
 */
function judgeArray(flag,evalDate,evalCnt) {
	if(!evalCnt) {
		evalCnt = 0;
	}
	for (var i = 0; i < XData.length; i++) {
		if(XData[i].indexOf(evalDate)>=0) {
			if(flag == 1) { //好评
				goodYData[i]=evalCnt;
			} else if(flag == 2) { //中评
				middleYData[i]=evalCnt;
			} else { //差评
				badYData[i]=evalCnt;
			}
		} else {
			if(flag == 1) { //好评
				if(!goodYData[i]){
					goodYData[i] = 0;
				}
			} else if(flag == 2) { //中评
				if(!middleYData[i]){
					middleYData[i] = 0;
				}
			} else { //差评
				if(!badYData[i]){
					badYData[i] = 0;
				}
			}
		}
	}
}

// 折线图显示
function showChart(type,goodData,middleData,badData) {
	if(type == 2) {
		XData = getWeekArr();
	} else if(type == 3) {
		XData = getLastMonths(6);
	} else if(type == 4) {
		XData = getQuarterArr();
	} else {
		XData = getDateArr();
	}
	goodYData = [],middleYData = [],badYData = [];
	var chartDiv = document.getElementById('chartShow');
	var myChart = echarts.init(chartDiv);
	if(null != goodData) {
		for ( var i = 0; i < goodData.length; i++) {
			judgeArray(1,goodData[i].EVALDATE,goodData[i].EVALCNT);
		}
	}
	if(null != middleData) {
		for ( var i = 0; i < middleData.length; i++) {
			judgeArray(2,middleData[i].EVALDATE,middleData[i].EVALCNT);
		}
	}
	if(null != badData) {
		for ( var i = 0; i < badData.length; i++) {
			judgeArray(3,badData[i].EVALDATE,badData[i].EVALCNT);
		}
	}
	// 参数设置
	option = {
		grid:{
			x:40,
			x2:40,
			y:25,
			height:135
		}, 
		tooltip : {
			trigger: 'axis'
		},
		legend : {
			data : [ '好评', '中评', '差评' ]
		},
		xAxis : [ {
			type : 'category',
			boundaryGap : false,// 坐标轴留白策略
			data : XData
		} ],
		yAxis : [ {
			type : 'value'
		} ],
		series : [ {
			name : '好评',
			type : 'line',
			data : goodYData
		}, {
			name : '中评',
			type : 'line',
			data : middleYData
		}, {
			name : '差评',
			type : 'line',
			data : badYData
		} ]
	};
	myChart.setOption(option);
}



function _changeBgmStyle(stl,obj) {
	var $obj = $(obj);
	$obj.removeClass(stl).addClass(stl+"_1");
	var $spTitle = $obj.children("div");
	$spTitle.removeClass("sp_title").addClass("sp_title_2");
	var pArr = $spTitle.children("p");
	$(pArr.get(0)).removeClass("sp_p1").addClass("sp_pp");
	$(pArr.get(2)).hide();
}

function _changeBgmStyle1(stl,obj) {
	var $obj = $(obj);
	$obj.removeClass(stl + "_1").addClass(stl);
	var $spTitle = $obj.children("div");
	$spTitle.removeClass("sp_title_2").addClass("sp_title");
	var pArr = $spTitle.children("p");
	$(pArr.get(0)).removeClass("sp_pp").addClass("sp_p1");
	$(pArr.get(2)).show();
}

//商品销售汇总分析（金额、订单、商品、浏览）
function _getDetailListPage(type) {
	var shopId = $("#hidShopId").val();
	var dateType = $("#dateType").val();
	var url = path + '/saleAmountList/toSaleAmountPage.do?dateType='+dateType;
	if(type == '1'){//金额
		url = path + '/saleAmountList/toSaleAmountPage.do?dateType='+dateType;
	}else if(type == '2'){//订单
		url = path + '/saleOrderList/toSaleOrderPage.do?dateType='+dateType;
	}else if(type == '3'){//商品
		url = path + '/orderDetail/toOrderDetailPage.do?dateType='+dateType;
	}else if(type == '4'){//浏览
		url = path + '/visitLog/toVisitLogPage.do?dateType='+dateType;
	}
	parent._IframeGotoPage('rightFrame',url);
	//window.location.href = url;
}
